//
//  DMLeftViewController.m
//  MCNKuaiYi
//
//  Created by 何典钊 on 2017/1/7.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMLeftViewController.h"
#import "DMLoginView.h"
#import "DMLeftViewCell.h"
#import "DMRegisteredViewController.h"
#import "DMTabBarViewController.h"
#import "DMSuperloginController.h"

static NSString *blueCell = @"blueCell";

@interface DMLeftViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *dmTableView;


@end

@implementation DMLeftViewController {
    NSMutableArray *_imgArr;
    NSMutableArray *_controllerArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.view.backgroundColor = DMRGBCOLOR;
    
    _imgArr = [NSMutableArray array];
    _controllerArr = [NSMutableArray array];
    [self setupUI];
    
    [self loadTable];
    
    NSNotificationCenter *registeredFication = [NSNotificationCenter defaultCenter];
    [registeredFication addObserver:self selector:@selector(pushRegisteredController) name:@"registeredFication" object:nil];
    
    NSNotificationCenter *loginFication = [NSNotificationCenter defaultCenter];
    [loginFication addObserver:self selector:@selector(pushLogincontroller) name:@"loginFication" object:nil];
    
}

//跳转到注册界面
- (void)pushRegisteredController {
    //关闭抽屉效果
    UITabBarController *tabBar = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *Navigation = tabBar.selectedViewController;
    DMRegisteredViewController *registered = [[DMRegisteredViewController alloc] init];
    [Navigation pushViewController:registered animated:NO];
    [self.drawer close];
}
//跳转登录界面
- (void)pushLogincontroller {
   //关闭抽屉效果
    UITabBarController *tabBar = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *Navigation = tabBar.selectedViewController;
    DMSuperloginController *loginVC = [[DMSuperloginController alloc] init];
    [Navigation pushViewController:loginVC animated:NO];
    [self.drawer close];
}

- (void) loadTable {
    NSArray *arr = @[@"名医申请",@"病例管理",@"关注医生",@"设置与帮助",@"注销"];
    for (NSString *str in arr) {
        [_imgArr addObject:str];
    }
    
    NSArray<NSString *> *controllerName = @[@"DMJDoctorApplyViewController",@"DMIllnessCaseManageViewController",@"DMAttentionDoctorViewController",@"DMSettingAndHelpViewController"];
    
    for (NSString *conName in controllerName) {
        [_controllerArr addObject:conName];
    }
    
}

#pragma mark - 数据源
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMLeftViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blueCell forIndexPath:indexPath];
    cell.illName.text = _imgArr[indexPath.row];
    cell.imgV.image = [UIImage imageNamed:_imgArr[indexPath.row]];
    cell.backgroundColor = DMRGBCOLOR;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _imgArr.count;
}

#pragma mark - 代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_dmTableView deselectRowAtIndexPath:indexPath animated:YES];
//     NSLog(@"%ld行被点击了 %@",(long)indexPath.row,_controllerArr[indexPath.row]);
    [self pushViewContollerName:_controllerArr title:_imgArr indexpath:indexPath];
}


#pragma mark - 搭建界面
- (void) setupUI {
    //头部试图
    DMLoginView *loginV = [[DMLoginView alloc] initLoginView];
    loginV.backgroundColor = DMRGBCOLOR;
    [self.view addSubview:loginV];
    
    [loginV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuide).offset(20);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view).offset(-120);
        make.height.offset(97);
    }];
    //tableView
    _dmTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _dmTableView.backgroundColor = DMRGBCOLOR;
    //注册
    [_dmTableView registerClass:[DMLeftViewCell class] forCellReuseIdentifier:blueCell];
    _dmTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //设置代理
    _dmTableView.dataSource = self;
    _dmTableView.delegate = self;
//    _dmTableView.hidden = YES;
    [self.view addSubview:_dmTableView];
    //    _dmTableView.tableHeaderView = loginV;
    
    [_dmTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(loginV);
        make.top.equalTo(loginV.mas_bottom);
        make.height.offset(600);
    }];
}

- (void) pushViewContollerName:(NSMutableArray *)name title:(NSMutableArray *)title indexpath:(NSIndexPath *)index {
    UITabBarController *tabBar = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *nav = tabBar.selectedViewController;
    Class cls = NSClassFromString(name[index.row]);
    UIViewController *code = [[cls alloc]init];
    code.hidesBottomBarWhenPushed = YES;
    code.navigationItem.title = title[index.row];
    
    [nav pushViewController:code animated:YES];
    [DMTools dmShowSVTitle:@"正在加载" andTime:1.0];
    [self.drawer close];
}


@end
